home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
os2
/
sidis100.zip
/
USERCTRL
/
USERCTRL.CMD
< prev
next >
Wrap
OS/2 REXX Batch file
|
1996-05-25
|
26KB
|
900 lines
/*************************************************************************/
/* */
/* REXX Control Script for Modem Handling and User Access Control */
/* */
/* (C) 1995, 1996 Axel Mueller (amueller@stargate.rz.fh-offenburg.de) */
/* Version 1.0 Last modified: 05/24/96 */
/*************************************************************************/
/* ##########################*/
/* ModemIndex = 1 means COM1 */
/* ModemIndex = 2 means COM2 */
/* ModemIndex = 3 means COM3 */
/* ModemIndex = 4 means COM4 */
/* ##########################*/
parse arg ModemIndex
call on error
/************************************************************************/
/* */
/* Setup */
/* */
/************************************************************************/
/* boolean variables */
True = 1
False = 0
/* general */
ModemNumber = value('MODEMNUMBER',,'OS2ENVIRONMENT')
/**************/
/* Path setup */
/**************/
/* Root directory: c:\userctrl */
Path = 'c:\userctrl'
/* Path to log files */
LogPath = value('ONLINELOGDIR',,'OS2ENVIRONMENT')
/* Directory under which all the POP mail directories are situated */
MailPath = value('MAILDIR',,'OS2ENVIRONMENT')
/* Path to IBMs TCP/IP ETC directory */
ETCPath = value('ETC',,'OS2ENVIRONMENT')
/* ##### Log files ##### */
/* Log file for all messages generated by this REXX script */
SystemLog = LogPath'\UserCtl'ModemIndex'.log'
/* ##### Files needed for login procedure ##### */
/* File to be send to modem user after CONNECT but before login */
WelcomeFile = Path'\welcome.msg'
/* ##### TCP/IP related ##### */
/* Configuration file for PPP driver of IBM's TCP/IP */
PPP_CFGFile = ETCPath'\ppp'ModemIndex'.cfg'
/******************/
/* COM port setup */
/******************/
PortName = 'com'ModemIndex
PortHandle = ''
WriteLim = 50
ReadLim = 50 /* 0,5 seconds timout */
Flags1 = '00001001'
Flags2 = '10100000'
Flags3 = '11010010'
ErrChar = '00'
BrkChar = '00'
XonChar = '11'
XoffChar = '13'
EnhParms = '00000010'
crlf = D2C(13)''D2C(10)
none_on = '00'
dtr_on = '01'
rts_on = '02'
both_on = '03'
none_off = 'FF'
dtr_off = 'FE'
rts_off = 'FD'
both_off = 'FC'
Timeout = 15 /* Wait 15 seconds for com port */
UserMaxLen = 25
PwdMaxLen = 25
'@echo off'
'cd' Path
call flagfile('START')
/************************************************************************/
/********************* ***********************************/
/********************* Main function ***********************************/
/********************* ***********************************/
/************************************************************************/
do forever
/* open COM port */
call open_com_port
/* set modified Device Control Block (dcb) */
rc = RxIOCtlSetDcbParm( PortHandle, WriteLim, ReadLim, Flags1, Flags2, Flags3, ErrChar, BrkChar, XonChar, XoffChar )
if rc <> 0 then
call log('MAINFUNCT : ###ERROR### RxIOCtlSetDcbParm failed with rc='rc)
else
call log('MAINFUNCT : modified Device Control Block set')
/* set Enhanced Parameters */
rc = RxIOCtlSetEnhParm( PortHandle, EnhParms )
if rc <> 0 then
call log('MAINFUNCT : ###ERROR### RxIOCtlSetEnhParm failed with rc='rc)
else
call log('MAINFUNCT : Enhanced parameter set')
/* initialize modem */
call modem_init
/* create flag file indicating that the system is waiting for caller */
call flagfile('WAIT')
/* wait for input from buffer; RING will be assumed */
Buffer = ""
Ring = False
call log('MAINFUNCT : waiting for RING ...' )
rc = lineout(SystemLog)
rc = RxIOCtlRead(PortHandle, 0, -1, 'Buffer')
if rc <> 0 then
call log('MAINFUNCT : ###ERROR### RxIOCtlRead failed with rc='rc)
BufferEmpty = True
do while BufferEmpty
if length(Buffer) > 3 then
BufferEmpty = False
else
do
Buffer = ''
rc = RxIOCtlRead(PortHandle, 0, 1, 'Buffer')
if rc <> 0 then
call log('MAINFUNCT : ###ERROR### RxIOCtlRead failed with rc='rc)
end
end
Buffer = strip(Buffer,,D2C(10))
Buffer = strip(Buffer,,D2C(13))
call log('MAINFUNCT : 'Buffer )
/* create flag file indicating that login is in process */
call flagfile('LOGIN')
if Buffer == 'RING' then
do /* pick up the line */
Rem = 0
rc = RxIOCtlWrite( PortHandle, 'ATA'||D2C(13), 'Rem' )
if rc <> 0 then
call log('MAINFUNCT : ###ERROR### RxIOCtlWrite (ATA) failed with rc='rc)
/* wait for CONNECT or NO CARRIER; Timeout = 70 seconds */
Connect = False
Cycle = 0
MaxCycle = 70 * 100 / ReadLim
do while (Connect == False) & (Cycle < MaxCycle)
Cycle = Cycle + 1
rc = RxIOCtlRead( PortHandle, 0, 1, 'Buffer' )
if length(Buffer) > 3 then
do
Buffer = strip(Buffer,,D2C(10))
Buffer = strip(Buffer,,D2C(13))
if left(Buffer, 7) == "CONNECT" then
Connect = True
if Buffer == "NO CARRIER" then
Cycle = MaxCycle
end
end
call log('MAINFUNCT : 'Buffer)
call check_carrier
if Carrier then
call handle_login /* start login procedure */
else
call log('MAINFUNCT : !!WARNING!! Timeout waiting for CONNECT')
end
/* close COM-Port */
call close_com_port
/* wait 15 seconds to force line down */
call log('MAINFUNCT : Waiting for 15 seconds ...')
call SysSleep 15
end
/* exit programm */
exit
/************************************************************************/
/********************* ***********************************/
/********************* Functions ***********************************/
/********************* ***********************************/
/************************************************************************/
/************************************************************************/
/* handle_login() */
/* */
/* main fuction handling the login procedure */
/************************************************************************/
handle_login:
call check_carrier
if Carrier then
call welcome_screen /* send welcome screen */
call check_carrier
LoginOkay = False
Attempt = 1
do while (LoginOkay = False) & (Attempt<=3) & Carrier
call log('HNDLELOGIN: --- 'Attempt'. Attempt ---' )
call check_carrier
if Carrier then
User = get_user()
else
call log( 'HNDLELOGIN: !!WARNING!! Carrier lost. Skipping user request' )
call check_carrier
if Carrier then
Password = get_password()
else
call log( 'HNDLELOGIN: !!WARNING!! Carrier lost. Skipping password request' )
call flush_buffer
call check_buffer
call check_carrier
if Carrier & BufferIsEmpty then
LoginOkay = check( User, Password )
else
do
call log( 'HNDLELOGIN: !!WARNING!! Carrier lost or unexpected characters in input buffer')
call log( 'HNDLELOGIN: !!WARNING!! Skipping user authentication' )
end
Attempt = Attempt + 1
end
if LoginOkay = True then
do
call check_carrier
if Carrier then
do
/* Work-around for PPP routing bug */
if ActiveUsers > 1 then do
NetstatLOG = LogPath'\netstat'ModemIndex'.log'
'netstat -r >'NetstatLog
'start 'Path'\reestab.cmd 'ModemIndex
end
call send( '$%' )
call send( crlf )
/* built file name for user log */
UserLog = LogPath'\'substr(date(european),4,2)||substr(date(european),7,2)'_'ModemIndex'.log'
call log('HNDLELOGIN: *** Startup PPP driver ***')
StartTime = time(normal)
StartDate = date(european)
rc = time('R')
'PPP file' PPP_CFGFile
LoginTime = trunc(time('R'),0)
EndTime = time(normal)
call log('HNDLELOGIN: *** Shutdown PPP driver ***')
/* Log connection data */
LogString = substr(User,1,20)||StartDate' 'StartTime' 'EndTime
rc = lineout( UserLog, LogString )
rc = lineout( UserLog )
rc = RxPrfAddLoginTime(User, LoginTime)
/* create flag file indicating that a user is active */
call flagfile('LGOUT')
/* remove routing table entries like "xxx.yyy.zzz.11 xxx.yyy.zzz.11 ppp1" */
Line = linein(PPP_CFGFile)
do while(pos(":", Line) == 0)
Line = linein(PPP_CFGFile)
end
rc = lineout(PPP_CFGFile)
RemoteIP = substr(Line, pos(":", Line)+1)
'route delete 'RemoteIP' 'RemoteIP
end
end
else
call log( 'HNDLELOGIN: Login failed. PPP was not started.' )
return
/************************************************************************/
/* welcome_screen () */
/* */
/* sends a text file (Welcom Screen) to modem user BEFORE user and */
/* password are requested */
/************************************************************************/
welcome_screen:
if length(stream(WelcomeFile, 'c', 'query exists')) > 0 then
do
Line = linein(WelcomeFile, 1, 1 )
call check_carrier
LineCount = 0
do while (Line <> '$') & Carrier
call check_carrier
if Carrier then
do
call send( Line )
call send( crlf )
LineCount = LineCount + 1
end
Line = linein(WelcomeFile)
end
rc = lineout(WelcomeFile)
if Carrier then
call log('WELCSCREEN: Welcome screen ('LineCount' lines) sent')
else
call log('WELCSCREEN: !!WARNING!! Welcome screen not (fully) sent')
end
return
/************************************************************************/
/* get_user() */
/* */
/* requests the username from modem user; there is a timeout after */
/* which the login procedure is canceled and the line is dropped */
/* (set in "Timeout"); there is also a maximum number of characters */
/* read from com port for username (set in "UserMaxLen") */
/************************************************************************/
get_user:
call check_buffer
if BufferIsEmpty then
call send(' Login:')
Dummy = 0
User = ''
UserLen = 0
Cycle = 0
UserTimeout = False
TooLong = False
Buffer = ''
do while (Buffer <> D2C(13)) & (UserTimeout == False) & (TooLong == False) & Carrier & BufferIsEmpty
call check_carrier
if Carrier then
do
Buffer = ''
rc = RxIOCtlReadChar( PortHandle, 1, 'Buffer' )
select
when rc = 0 then
if UserLen < UserMaxLen then
do
User = User||Buffer
UserLen = UserLen + 1
Cycle = 0
end
else
TooLong = True
when rc = -1 then /* send no echo for CR */
Dummy = 1
when rc = -2 then
Cycle = Cycle + 1
end
MaxCycle = Timeout * 100 / ReadLim
if Cycle > MaxCycle then
UserTimeout = True
call check_carrier
end
end
if Carrier & BufferIsEmpty then
call send( crlf )
if UserTimeout = True then
do
call log( 'GETUSER : !!WARNING!! Timeout waiting for username. Timout: 'Timeout's')
call check_carrier
if Carrier then
call hangup
end
if TooLong = True then
do
call log( 'GETUSER : !!WARNING!! Username length exceeded. Maximum length: 'UserMaxLen )
call check_carrier
if Carrier then
call hangup
end
else
if Carrier then
call log( 'GETUSER : User =' User )
return User
/************************************************************************/
/* get_password() */
/* */
/* requests the password from modem user; there is a timeout after */
/* which the login procedure is canceled and the line is dropped */
/* (set in "Timeout"); there is also a maximum number of characters */
/* read from com port for username (set in "PwdMaxLen") */
/************************************************************************/
get_password:
call check_buffer
if BufferIsEmpty then
call send('Password:')
Dummy = 0
Password = ''
PwdLen = 0
Cycle = 0
PwdTimeout = False
TooLong = False
Buffer = ''
do while (Buffer <> D2C(13)) & (PwdTimeout == False) & (TooLong == False) & Carrier & BufferIsEmpty
call check_carrier
if Carrier then
do
Buffer = ''
rc = RxIOCtlReadChar( PortHandle, -1, 'Buffer' )
select
when rc = 0 then
if PwdLen < PwdMaxLen then
do
Password = Password||Buffer
PwdLen = PwdLen + 1
Cycle = 0
end
else
TooLong = True
when rc = -1 then /* send no echo for CR */
Dummy = 1
when rc = -2 then
Cycle = Cycle + 1
end
MaxCycle = Timeout * 100 / ReadLim
if Cycle > MaxCycle then
PwdTimeout = True
call check_carrier
end
end
if Carrier & BufferIsEmpty then
call send( crlf )
if PwdTimeout = True then
do
call log( 'GETPASSWD : !!WARNING!! Timeout waiting for password. Timeout: 'Timeout's' )
call check_carrier
if Carrier then
call hangup
end
if TooLong = True then
do
call log( 'GETPASSWD : !!WARNING!! Password length exceeded. Maximum length: 'PwdMaxLen )
call check_carrier
if Carrier then
call hangup
end
else
if Carrier then
call log( 'GETPASSWD : Password =' copies('*',PwdLen) )
return Password
/************************************************************************/
/* check(User,Password) */
/* */
/* checks username and password given by modem user */
/************************************************************************/
check:
parse arg User, Password
LoginOkay = RxPrfCheckPassword(User, Password)
if LoginOkay then
do
call log( 'CHECK : User identification passed' )
call send( crlf )
call send('Login OK.')
call send( crlf )
call send( crlf )
LastLogin = 'Last logout on 'RxPrfGetLastLoginDate(User)
call send(LastLogin)
call send( crlf )
UserMail = MailPath'\'User'\*.msg'
call SysFileTree UserMail, 'MailFiles', 'O'
call send( crlf )
if MailFiles.0 > 0 then
call send('*** You have 'MailFiles.0' new mail(s) ***')
else
call send('No new mail.')
call send( crlf )
call send( crlf )
UserRealName = RxPrfGetRealName(User)
/* create flag file indicating that a user is active */
call flagfile('ACTIV')
ActiveUsers = 0
do Index=1 to ModemNumber
ActivFlagFile = Path'\L'Index'_ACTIV.FLG'
if length(stream(ActivFlagFile, 'c', 'query exists')) > 0 then
do
ActiveUsers = ActiveUsers + 1
Line = linein(ActivFlagFile)
rc = lineout(ActivFlagFile)
end
else
Line = 'not used'
call send( 'Modem 'Index' : 'Line )
call send( crlf )
end
call send( crlf )
end
else
do
call log( 'CHECK : !!WARNING!! User authentication failed' )
call send('Login failed.')
call send( crlf )
FailLog = Path'\log\'substr(date(european),4,2)||substr(date(european),7,2)'_'ModemIndex'XX.LOG'
LogString = date(european)' 'time(normal)' U='User' P ='copies('*', length(Password))
rc = lineout( FailLog, LogString )
rc = lineout( FailLog )
end
return LoginOkay
/************************************************************************/
/* send ( sendstring ) */
/* */
/* send a character string off to the modem */
/************************************************************************/
send:
parse arg SendString
Rem = 0
call check_carrier
if Carrier then
do
rc = RxIOCtlWrite( PortHandle, SendString, 'Rem' )
if rc <> 0 then
say 'SEND : ###ERROR### RxIOCtlWrite failed with rc='rc
end
else
do
if SendString == crlf then
say 'SEND : !!WARNING!! crlf not sent'
else
say 'SEND : !!WARNING!! 'SendString' not sent'
end
return
/************************************************************************/
/* log ( logstring ) */
/* */
/* logs the main activities of this REXX script */
/************************************************************************/
log:
parse arg LogString
LogString = strip(LogString,,D2C(13))
rc = lineout( SystemLog, date(european)'-'time(normal)'' LogString )
if rc <> 0 then
say 'LOG : ###ERROR### lineout failed with rc='rc
say date(european)' 'time(normal)' 'left(LogString,61)
return
/************************************************************************/
/* flagfile ( Flag ) */
/* */
/* creates a flag file indicating current status of UserCtrl program */
/************************************************************************/
flagfile:
parse arg Flag
/* delete old flag file */
FlagFile = Path'\L'ModemIndex'_START.FLG'
if length(stream(FlagFile, 'c', 'query exists')) > 0 then
rc = SysFileDelete(FlagFile)
FlagFile = Path'\L'ModemIndex'_WAIT.FLG'
if length(stream(FlagFile, 'c', 'query exists')) > 0 then
rc = SysFileDelete(FlagFile)
FlagFile = Path'\L'ModemIndex'_LOGIN.FLG'
if length(stream(FlagFile, 'c', 'query exists')) > 0 then
rc = SysFileDelete(FlagFile)
FlagFile = Path'\L'ModemIndex'_ACTIV.FLG'
if length(stream(FlagFile, 'c', 'query exists')) > 0 then
rc = SysFileDelete(FlagFile)
FlagFile = Path'\L'ModemIndex'_LGOUT.FLG'
if length(stream(FlagFile, 'c', 'query exists')) > 0 then
rc = SysFileDelete(FlagFile)
FlagFile = Path'\L'ModemIndex'_'Flag'.FLG'
if Flag == "ACTIV" then
rc = lineout(FlagFile, UserRealName)
rc = lineout(FlagFile)
return
/************************************************************************/
/* open_com_port() */
/* */
/* opens the com port */
/************************************************************************/
open_com_port:
rc = RxIOCtlOpenPort(PortName, 'PortHandle')
if rc <> 0 then
do
call log('COMOPEN : ###ERROR### 'PortName' RxIOCtlOpenPort failed with rc='rc)
exit
end
else
call log('COMOPEN : 'PortName' opened')
return
/************************************************************************/
/* close_com_port() */
/* */
/* closes the com port */
/************************************************************************/
close_com_port:
rc = RxIOCtlClosePort( PortHandle )
if rc <> 0 then
do
call log('COMCLOSE : ###ERROR### 'PortName' RxIOCtlClosePort failed with rc='rc)
exit
end
else
call log('COMCLOSE : 'PortName' closed')
return
/************************************************************************/
/* modem_init () */
/* */
/* initializes the modem; note that the modem should be set to NO */
/* auto answer (ATS0=0) since the script picks up the line if it */
/* detects a RING signal. Therefore the modem has to be set for */
/* echo. (ATE=1) */
/************************************************************************/
modem_init:
call flush_buffer
/* ATZ Initialize modem */
/* Q0 Modem returns result code */
/* V1 Display result codes in verbose form */
/* S0=0 disable auto answer */
/* S3=13 Carriage return = ASCII 13 */
/* S4=10 Line feed = ASCII 10 */
/* S7=60 number of seconds the modem waits for a carrier */
/* S10=7 wait 7/10 seconds after loss of carrier before hangup */
Rem = 0
rc = RxIOCtlWrite( PortHandle, 'ATZQ0V1S0=0S3=13S4=10S7=60S10=7'||D2C(13), 'Rem' )
if rc <> 0 then
call log('MODEM_INIT: ###MODEM ERROR### RxIOCtlWrite failed with rc='rc)
else
call log('MODEM_INIT: ATZ sent to modem')
call SysSleep 1
InitNotOK = True
do while InitNotOK
BufferEmpty = True
do while BufferEmpty
Buffer = ''
rc = RxIOCtlRead(PortHandle, 0, 1, 'Buffer')
if rc <> 0 then
call log('MODEM_INIT: ###MODEM ERROR### RxIOCtlRead failed with rc='rc)
else
if length(Buffer) > 3 then
do
BufferEmpty = False
end
end
if Buffer == "OK"||crlf then
do
call log('MODEM_INIT: OK received from modem')
InitNotOK = False
end
else
call log('MODEM_INIT: ###MODEM ERROR### Expected:OK Received:'Buffer)
end
return
/************************************************************************/
/* hangup() */
/* */
/* forces the modem to drop the line */
/************************************************************************/
hangup:
call flush_buffer
Rem = 0
rc = RxIOCtlWrite(PortHandle, '+++', 'Rem')
if rc <> 0 then
call log('HANGUP : ###ERROR### Could not send escape sequence (+++) to modem')
else
call log('HANGUP : escape sequence (+++) sent to modem' )
call SysSleep 2
Rem = 0
rc = RxIOCtlWrite(PortHandle, 'ATH0'||crlf, 'Rem')
if rc <> 0 then
call log('HANGUP : ###ERROR### Could not send hangup command (ATH0) to modem')
else
call log('HANGUP : hangup command (ATH0) sent to modem')
call SysSleep 10
return
/************************************************************************/
/* check_buffer () */
/* */
/* checks the input buffer of the com port for characters */
/************************************************************************/
check_buffer:
/* there should be NO characters in the input buffer */
RxCount = 0
RxSize = 0
rc = RxIOCtlGetRxCount(PortHandle, 'RxCount', 'RxSize')
if RxCount > 0 then
do
BufferIsEmpty = False
call log( 'CHCKBUFFER: !!WARNING!! Unexpected characters in input buffer found')
call log( 'CHCKBUFFER: !!WARNING!! number of characters: 'RxCount )
call log( 'CHCKBUFFER: !!WARNING!! size of input buffer: 'RxSize )
end
return
/************************************************************************/
/* flush_buffer () */
/* */
/* flushes the input buffer */
/************************************************************************/
flush_buffer:
rc = RxIOCtlFlushInput( PortHandle )
call log( 'FLUSHBUFFR: Input buffer flushed' )
BufferIsEmpty = True
return
/************************************************************************/
/* check_carrier () */
/* */
/* checks if the line is up and there is a carrier */
/************************************************************************/
check_carrier:
rc = RxIOCtlDetectCarrier( PortHandle )
if rc = 0 then
Carrier = True
else
do
Carrier = False
call log('CHKCARRIER: !!WARNING!! Carrier lost')
end
return
/************************************************************************/
/* error () */
/* */
/* */
/************************************************************************/
error:
call log('ERROR : ###ERROR### error() called')
call RxIOCtlClosePort PortHandle
exit